var call = require("../../utils/request.js")
Page({
  data: {
    showtip:true,
    latitude: '',
    longitude: '',
    clickname:'',
    scrollTop: 0,
    hosuselist:[],
    hid:'',
    loca:[],
    address: "请选择地址",
    //入住日期
    order_date: "",
    orderDay: "请选择入住日期",
    dizhi: [],
    centerX: '',
    centerY: '',
    weishouquan:false,
    markers: [],
    showDialog: false,
    mapId: "myMap", //wxml中的map的Id值,
    picList:[
    ]
  },
  // 点击回到原点
  moveTolocation: function () {
 
    let Id = this.data.mapId
    var mapCtx = wx.createMapContext(Id);
    mapCtx.moveToLocation();

  },

  onReady: function (e) {
    // 使用 wx.createMapContext 获取 map 上下文 
    this.mapCtx = wx.createMapContext('myMap')
  },
  onLoad: function () {


    var url = 'gethuodongimage'
    call.getData(url, this.getimagelist, this.fail)
    
    if (wx.getStorageSync("uid")) {
    
      var url = 'userNoPay?uid=' + wx.getStorageSync("uid")
      call.getData(url, this.userNoPay, this.fail)



    } else {
      // wx.showModal({
      //   title: '提示',
      //   content: '您尚未登录,是否立即登录？',
      //   showCancel: true,//是否显示取消按钮
      //   cancelText: "取消",//默认是“取消”
      //   cancelColor: '#333',//取消文字的颜色
      //   confirmText: "登录",//默认是“确定”
      //   confirmColor: '#333',//确定文字的颜色
      //   success: function (res) {
      //     wx.redirectTo({
      //       url: '../hoteldetail/hoteldetail',
      //     })
      //   },
      // })
    }

    // var that = this
    //  //控制显示“未”
    // setTimeout(function(){
       
    //   that.showtip = false;
    // }, 3000);



    wx.showShareMenu({

      withShareTicket:true,
      
      menus:['shareAppMessage','shareTimeline']
      
      })

      
    let that = this

    setTimeout(() => {
      console.log(93)
   

      this.setData({
        showtip: false,
   
      })

    }, 3000);


    wx.getSetting({
    
      success: res => {

        // 已授权
        if (res.authSetting['scope.address']) {
          console.log('已授权定位')
          wx.getLocation({
            type: 'gcj02', //返回可以用于wx.openLocation的经纬度
            success: (res) => {
              var that = this;
              var uid = 0;
              if (wx.getStorageSync("uid")) {
                uid = wx.getStorageSync("uid")
              }
              var url = 'getHotels?lng='+res.longitude+'&&lat='+res.latitude;
              call.getData(url, this.getData, this.fail)
      
              var url2 = 'getHouse?uid='+uid+'&&lng=' + res.longitude + '&&lat=' + res.latitude;
              call.getData(url2, this.getHouse, this.fail)
      
              let latitude = res.latitude;
              let longitude = res.longitude;
       
              let marker = this.createMarker(res);
              this.setData({
                centerX: longitude,
                centerY: latitude,
              })
            }
          });
 
        } else {
          console.log('尚未授权')
          
          wx.showModal({  
            title: '您未开启地理位置授权',  
            content: '开启地理位置授权才能为您推荐附近优质房源哦!',  
            success: function(res) {  
                if (res.confirm) {  
                console.log('用户点击确定')  
                wx.openSetting()
                } else if (res.cancel) {  
                console.log('用户点击取消')  
                }  
            }  
        })  

        } 
      }
    })

  

  },

  
  getimagelist: function (data) {
    this.setData({
      picList: data.data
    })

    
  },
  

  previewImg: function (data) {
    wx.reLaunch({
      url: '../mine/buycards',
    })
    
  },


  userNoPay: function (data) {
    console.log(4576)
    console.log(data.data)
    if(data.data > 0)
    this.setData({
      weizhifu: true
    })
  },
  reset: function (latitude,longitude){
        this.setData({
          centerX: longitude,
          centerY: latitude,
        })

    var url = 'getHotels?lng=' + longitude + '&&lat=' + latitude;
    call.getData(url, this.getData, this.fail)

    var uid = 0;
    if (wx.getStorageSync("uid")) {
      uid = wx.getStorageSync("uid")
    }

    var url2 = 'getHouse?uid='+uid+'&&lng=' + longitude + '&&lat=' + latitude;
    call.getData(url2, this.getHouse, this.fail)
  },
  getHouse: function (data) {

    this.setData({
      hosuselist: data.data
    })
  },

  gohome: function (data) {
    wx.reLaunch({
      url: '../index/index',
    })
  },


  golist: function (data) {
    wx.reLaunch({
      url: '../order_list/order_list?current=0',
    })
  },

  getData: function (data) {
    
    var that = this;
    that.data.dizhi = data.data
    // console.log(that.data.dizhi)
    let markers = [];
    var lingyuanData = that.data.dizhi

    for (let item of lingyuanData) {

      let marker = this.createMarker(item);

      markers.push(marker)
    }
    this.setData({
      markers: markers
    })
  },


  fail: function () {
    console.log("失败")
  },
  regionchange(e) {
    // console.log(e.type)
  },


  // 点击标点获取数据
  markertap(e) {
    console.log(1112)
 
    var id = e.detail.markerId
    var that = this

    for (var i = 0; i < this.data.markers.length; i++) {
      if (this.data.markers[i].id == id){
       var name = this.data.markers[i].name
        var title = this.data.markers[i].title
        var img = this.data.markers[i].img 
        var km = this.data.markers[i].km 
        var id = this.data.markers[i].id 
        var h_address = this.data.markers[i].h_address 
        
      }
    }
    console.log(id)
  //  var name = this.data.markers[id - 1].name
   
    this.setData({
      hid:id,
      jiudianimg: img,
      jiudianName: title,
      km: km,
      h_address: h_address,
      showDialog: true,
    })
  },


  gourl: function (e) {
    console.log(123213)
    console.log(e.currentTarget.dataset.item);
    wx.navigateTo({
      url: '../hoteldetail/detail?hid=' + e.currentTarget.dataset.item,
    })
  },


  godetail: function (e) {
    console.log(e.currentTarget.dataset.item);
    wx.navigateTo({
      url: '../detail/detail?roomid=' + e.currentTarget.dataset.item.roominfo.roomid,
     
    })
  },

  toggleDialog: function () {
    console.log(1)
    this.setData({
      showDialog: false,
    })
  },

  openLocation: function openLocation() {
    var that = this;
    wx.chooseLocation({
      success: function success(res) {
       console.log(res);

        that.reset(res.latitude, res.longitude)
    
             // _this2.address = res.address;
      }
    });

  },
  navigateToDetail: function () {
    wx.navigateTo({
      url: "../detail/detail"
    });

  },



  // moveToLocation: function () {
  //   console.log(86)
  //   this.mapCtx.moveToLocation()
  // },
  createMarker(point) {
 
    let latitude = point.latitude;
    let longitude = point.longitude;
    let marker = {
      iconPath: "https://minshu.lvfree.net/uploads/20200917/hotel.png",
      id: point.id || 0,
      name: point.type || '',
      title: point.name || '',
      img:point.h_img,
      km: point.distance,
      h_address: point.h_address,
      latitude: latitude,
      longitude: longitude,
      width: 30,
      height: 30,
      label: {
        content: point.name+"\r\n房间数:"+point.type,
        bgColor: "#fff",
        fontSize:10,
        padding: "2px",
        borderRadius: "2px",
        borderWidth: "1px",
        borderColor: "#333",

      },
      callout: {
        content: point.type,
        fontSize: 0,
      }
    };
    return marker;

  }
})